public member function
<memory>

std::shared_ptr::operator->

element_type* operator->() const noexcept;
Dereference object member
Returns a pointer to the object pointed by the stored pointer in order to access one of its members.

This member function shall not be called if the stored pointer is a null pointer.

It returns the same value as get(). See shared_ptr::get for more details.

Parameters

none

Return value

A pointer to the object managed by the shared_ptr.
element_type is a member type, defined as an alias of shared_ptr's template parameter.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// shared_ptr::operator->
#include <iostream>
#include <memory>

struct C { int a; int b; };

int main () {
  std::shared_ptr<C> foo;
  std::shared_ptr<C> bar (new C);

  foo = bar;

  foo->a = 10;
  bar->b = 20;

  if (foo) std::cout << "foo: " << foo->a << ' ' << foo->b << '\n';
  if (bar) std::cout << "bar: " << bar->a << ' ' << bar->b << '\n';

  return 0;
}

Output:
foo: 10 20
bar: 10 20


See also